--- title: "一步之遥" created: 2025-11-28 tags: - 算法 --- # 一步之遥 ## 题目 [一步之遥](https://www.lanqiao.cn/paper/3863/problem/652/) ![[image-831e5995.png]] ## 思路分析 ![[image-c9b84c7d.png]] ## 代码实现 ```cpp #include using namespace std; #define endl '\n' const int N=1e6; int d[N]; bool isVaild(int x){ return x>=-N && x<=N && d[x]==-1; } int bfs(int u){ queue q; memset(d,-1,sizeof d); q.push(u); d[u]=0; while(!q.empty()){ auto cur=q.front();q.pop(); if(cur==1){ return d[cur]; } int choice1=cur+97,choice2=cur-127; if(isVaild(choice1)){ q.push(choice1); d[choice1]=d[cur]+1; } if(isVaild(choice2)){ q.push(choice2); d[choice2]=d[cur]+1; } } } int main() { ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); cout<